package xyz.fuqufyb.aec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.fuqufyb.aec.dao.RechargeMapper;
import xyz.fuqufyb.aec.data.TableResult;
import xyz.fuqufyb.aec.entity.Recharge;
import xyz.fuqufyb.aec.service.IRechargeService;
import xyz.fuqufyb.aec.utils.SnowflakeIdWorker;

import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: RechargeServiceImpl
 * @date 时间: 2021/2/2 13:20
 */
@Service("IRechargeService")
public class RechargeServiceImpl implements IRechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {

        int i = rechargeMapper.deleteByPrimaryKey(id);
        if (i > 0) {

            return i;
        }
        return 0;
    }

    @Override
    public int insertSelective(Recharge record) {
        /**
         * 新增生成 订单
         */
        record.setOrderId(SnowflakeIdWorker.nextId());

        int i = rechargeMapper.insertSelective(record);
        if (i > 0) {

            return i;
        }
        return 0;
    }

    @Override
    public Recharge selectByPrimaryKey(Integer id) {

        Recharge recharge = rechargeMapper.selectByPrimaryKey(id);

        if (recharge != null) {
            return recharge;
        }
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Recharge record) {
        int i = rechargeMapper.updateByPrimaryKeySelective(record);

        if (i > 0) {
            return i;
        }

        return 0;
    }
    @Override
    public TableResult getPageRecharge(Map<String, Object> maps) {
        String page = (String) maps.get("page");
        String limit = (String) maps.get("limit");
        if (page != null && limit != null) {
            PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));

            List<Recharge> pageRecharge = rechargeMapper.getPageRecharge(maps);

            PageInfo<Recharge> pageInfo = new PageInfo<>(pageRecharge);
            return  TableResult.getTableResultSuccess(pageInfo.getTotal(),pageInfo.getList());
        }

        return TableResult.getTableResultError("分页参数不能为空");
    }


    @Override
    public int deleteByRechargeIds(List<Integer> asList) {
        int i = rechargeMapper.deleteByRechargeIds(asList);
        if (i>0){
            return i;
        }

        return 0;
    }
}
